import java.util.Scanner;

public class Minesweeper {
	static Scanner sc = new Scanner(System.in);
	
	static void game(int[][] array){
		int row= array.length;
		int col= array[0].length;
		int [][] userArray = new int [row][col];
		int count=1;
		while(count>0){
			System.out.print("Enter a row:");
			int userRow = sc.nextInt();
			System.out.print("Enter a col:");
			int userCol = sc.nextInt();
//			while (!((userRow>=0&&userRow<userArray.length)&&(userCol>=0&&userCol<userArray[0].length))){
//				System.out.println("Please, enter for rows number between 0 and "+(userArray.length-1));
//				System.out.println("Please, enter for cols number between 0 and "+(userArray[0].length-1));
//				System.out.print("Enter rows:");
//				row=sc.nextInt(); //array.length
//				System.out.print("Enter colums:");
//				col=sc.nextInt(); //array[0].length
//			}
			if(array[userRow][userCol]==-1){
				System.out.println("You lose!");
				count=0;
			}
			else{
				userArray[userRow][userCol]=array[userRow][userCol];
				printArray(userArray,userArray.length, userArray[0].length);
			}
		}
	
	}

	static int search (int [][]array, int row, int col){
		int counter = 0;
		
		if((row - 1 >= 0)&& (array[row - 1][col] == -1)){
			counter++;
		}
		if((row+1<array.length)&&(array[row+1][col]==-1)){
			counter++;
		}
		if((col - 1 >= 0)&& (array[row][col-1] == -1)){
			counter++;
		}
		if((col + 1 < array[0].length)&& (array[row][col+1] == -1)){
			counter++;
		}
		if((row - 1 >= 0)&&(col - 1>= 0)&&(array[row-1][col-1]==-1)){
			counter++;
		}
		if((row + 1 < array.length)&&(col + 1< array[0].length)&&(array[row+1][col+1]==-1)){
			counter++;
		}
		if((row - 1 >= 0)&&(col + 1< array[0].length)&&(array[row-1][col+1]==-1)){
			counter++;
		}
		if((row +1 < array.length)&&(col - 1>= 0)&&(array[row+1][col-1]==-1)){
			counter++;
		}
		return counter;
	}
	static void printArray(int [][] array,int row, int col){
		System.out.print("       ");
		for (int i=0; i<col; i++){
			System.out.printf("  %2d   ",i);
		}
		System.out.println();
		for(int i= 0; i<row; i++){
			for (int j = -1; j<col; j++){
				if(j==-1){
					System.out.printf("  %2d   ",i);
				}else{
					System.out.printf("[ %2d ] ",array[i][j]);
				}
			}
			System.out.println();
			
		}
	}
	
	public static void main(String[] args) {
		System.out.print("Enter rows:");
		int row=sc.nextInt(); //array.length
		System.out.print("Enter colums:");
		int col=sc.nextInt(); //array[0].length
		int random;
		while (!((row>0)&&(col>0))){
			System.out.println("Please, enter a number larger than 0!!!");
			System.out.print("Enter rows:");
			row=sc.nextInt(); //array.length
			System.out.print("Enter colums:");
			col=sc.nextInt(); //array[0].length
		}
		int [][] array = new int [row][col];
		
		for(int i=0; i<row; i++){
			for(int j=0; j<col; j++){
				random = (int)(Math.random()*10);
				if(random<=3){
					array[i][j]=-1;
				}
				else{
					array[i][j]=0;
				}
			}
		}
		
		for(int i=0; i<row; i++){
			for(int j=0; j<col; j++){
				if(array[i][j]==0){
					array[i][j]=search(array,i,j);
				}
			}
		}
		printArray(array, array.length, array[0].length);
		
		game(array);
		
	}

}